package com.shuqing.interceptor;

import com.shuqing.domain.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: ShuqingLibrary
 * @ClassName ResourceInterceptor
 * @description:
 * @author: Ats
 * @create: 2025-03-31 14:14
 * @Version 1.0
 */
public class ResourceInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler) throws Exception {
        User crurrentUser = (User) request.getSession()
                .getAttribute("USER_SESSION");
        //如果当前session中保留的有User信息，那么代表你已经登陆，直接进入主页面，返回true
        if (crurrentUser != null) {
            return true;
        }
        //获取URI完整路径
        String uri = request.getRequestURI();
        //判断是否是登录页面的请求
        if (uri.indexOf("login") >0){
            return true;
        }
        request.setAttribute("msg", "您还没有登录，请先登录!");
        request.getRequestDispatcher("/admin/login.jsp")
                .forward(request,response);
        return false;
    }
}
